﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Globalization;

using System.Threading;
using System.Globalization;

using Common;


namespace CNCSoft.ErpApps.TMS
{
    public partial class UserControl_ChonLOP : UserControl
    {
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        private DataSet ds = new DataSet();

        private string strMaLop = "";

        public string MaLop
        // Retrieves the value of the private variable colBColor.
        {
            get
            {
                return strMaLop;
            }
            // Stores the selected value in the private variable colBColor, and 
            // updates the backcolor of the label control lblDisplay.
            set
            {
                strMaLop = value;
                //txtValue.Text = strRefField;
            }
        }
        
        private string strCon = "";
        private string strNienHoc = "";

        
        public UserControl_ChonLOP()
        {

            // Sets the UI culture to VietNamese (VietNam) / English
            if (GlobalVariable.WSInfo.Language == "Tiếng Việt")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
            else if (GlobalVariable.WSInfo.Language == "English")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
            
            InitializeComponent();
            strNienHoc = "";
            strCon = "";
        }

        
        public UserControl_ChonLOP(string strMaNienHoc, string strConnectionstring)
        {
            InitializeComponent();
            strNienHoc = strMaNienHoc;
            strCon = strConnectionstring;
        }

        private void UserControl_ChonLOP_Load(object sender, EventArgs e)
        {
            if (strCon == "" || strCon == null) return;

            txtMaNienHoc.Text = strNienHoc;
            //MessageBox.Show(strCon);
            //Danh muc lop
            
            dataAdapter = new SqlDataAdapter("SELECT * FROM DM_LOP WHERE MaNienHoc = '" + txtMaNienHoc.Text.Trim() + "' order by makhoi desc, STT", strCon);
            ds.Tables["DM_Lop"].Clear();
            dataAdapter.Fill(ds, "DM_Lop");
            
            cboMaLop.DataSource = ds.Tables["DM_Lop"];

            cboMaLop.ValueMember = "MaLop";
            cboMaLop.DisplayMember = "TenLop";
        }

        private void txtMaNienHoc_TextChanged(object sender, EventArgs e)
        {
            if (strCon == "" || strCon == null) return;

            try
            {
                string strCS = "";
                if (chkCS1.Checked) strCS = "'1'";
                if (chkCS2.Checked) strCS = "'2'";
                if (chkCS1.Checked && chkCS2.Checked) strCS = "'1', '2'";

                string strCap = "";
                if (chkCap1.Checked) strCap = "'1'";
                if (chkCap2.Checked) strCap = "'2'";
                if (chkCap3.Checked) strCap = "'3'";
                if (chkCap1.Checked && chkCap2.Checked) strCap = "'1', '2'";
                if (chkCap1.Checked && chkCap3.Checked) strCap = "'1', '3'";
                if (chkCap2.Checked && chkCap3.Checked) strCap = "'2', '3'";
                if (chkCap1.Checked && chkCap2.Checked && chkCap3.Checked) strCap = "'1', '2', '3'";

                string strSQLDMLOP = "SELECT distinct * FROM DM_LOP WHERE MaNienHoc = '" + txtMaNienHoc.Text + "'" +
                                     " AND MaCoSo IN (" + strCS + ") AND MaCap IN (" + strCap + ") order by makhoi desc, STT ";

                if (ds.Tables["DM_Lop"] != null)
                    ds.Tables["DM_Lop"].Clear();
                dataAdapter = new SqlDataAdapter(strSQLDMLOP, strCon);
                dataAdapter.Fill(ds, "DM_Lop");

            }

            catch { }
        }

        private void chkCS2_CheckedChanged(object sender, EventArgs e)
        {
            txtMaNienHoc_TextChanged(sender, e);
        }

        private void chkCS1_CheckedChanged(object sender, EventArgs e)
        {
            txtMaNienHoc_TextChanged(sender, e);
        }

        private void chkCap3_CheckedChanged(object sender, EventArgs e)
        {
            txtMaNienHoc_TextChanged(sender, e);
        }

        private void chkCap2_CheckedChanged(object sender, EventArgs e)
        {
            txtMaNienHoc_TextChanged(sender, e);
        }

        private void chkCap1_CheckedChanged(object sender, EventArgs e)
        {
            txtMaNienHoc_TextChanged(sender, e);
        }

        

        //Tao event de raise khi CbomaLop Thay doi
        // Declare an event 
        public event ValueChangedEventHandler ValueChanged;
        
        protected virtual void OnValueChanged(ValueChangedEventArgs e)
        {
            if (ValueChanged != null)
                ValueChanged(this, e);
        }

        //Declare a delegate 
        public delegate void ValueChangedEventHandler(object sender, 
                                  ValueChangedEventArgs e);

        public class ValueChangedEventArgs : EventArgs
        {
            public string NewValue { get; set; }

            public ValueChangedEventArgs(string newValue)
            {
                NewValue = newValue;
            }
        }

        private void cboMaLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboMaLop.SelectedIndex == -1 || cboMaLop == null) return;
            try
            {
                // Raise the event 
                OnValueChanged(new ValueChangedEventArgs(cboMaLop.SelectedValue.ToString()));
                MaLop = cboMaLop.SelectedValue.ToString();
            }
            catch { }
        }






    }
}
